package com.zsc.newbuds.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.MoneyOfWeekVo;
import com.zsc.newbuds.entity.StatisticOfWeekVo;
import com.zsc.newbuds.mapper.AdminMapper;
import com.zsc.newbuds.service.AdminService;
import com.zsc.newbuds.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author D
 */
@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AdminMapper adminMapper;


    @Override
    public HashMap<String, Object> getStatistic() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("totalBooksNumber", adminMapper.getTotalBooksNumber());
        map.put("totalAmount", adminMapper.getTotalAmount());
        map.put("totalUsers", adminMapper.getTotalUsers());
        map.put("totalWeight", adminMapper.getTotalWeight());
        map.put("totalClothesNumber", adminMapper.getTotalClothesNumber());
        return map;
    }

    @Override
    public Admin getAdminByAccount(String account) {
        String key = "admin:" + account;
        System.out.println(key + "Get" + " " + redisUtil.hasKey(key));
        if (redisUtil.hasKey(key)) {
            System.out.println("存在");
            return (Admin) redisUtil.get(key);
        } else {
            Admin admin = adminMapper.getAdminByAccount(account);
            redisUtil.set(key, admin);
            System.out.println("访问数据库");
            return admin;
        }
    }

    @Override
    public double[] getMoneyOfMonth() {

        double[] money = new double[12];
        List<MoneyOfWeekVo> moneyOfWeekVos = adminMapper.listMoneyOfWeekVo();

        for (MoneyOfWeekVo moneyOfWeekVo : moneyOfWeekVos) {
            money[moneyOfWeekVo.getMonthly() - 1] = moneyOfWeekVo.getSaleMoney();
        }
        return money;
    }

    @Override
    public HashMap<String, Object[]> getDonationStatistic() {

        Object[] cloth = {0, 0, 0, 0, 0, 0, 0};

        Object[] clothWeight = {0, 0, 0, 0, 0, 0, 0};

        Object[] booksNumber = {0, 0, 0, 0, 0, 0, 0};

        List<StatisticOfWeekVo> clothOfWeekVos = adminMapper.listClothStatisticOfWeekVo();
        for (StatisticOfWeekVo statistic : clothOfWeekVos) {
            cloth[statistic.getCurrentWeek() - 1] = statistic.getStatistic();
        }

        List<StatisticOfWeekVo> weightOfWeekVos = adminMapper.listWeightStatisticOfWeekVo();

        for (StatisticOfWeekVo statistic : weightOfWeekVos) {
            clothWeight[statistic.getCurrentWeek() - 1] = statistic.getStatistic();
        }

        List<StatisticOfWeekVo> bookOfWeekVos = adminMapper.listBooksNumberStatisticOfWeekVo();

        for (StatisticOfWeekVo statistic : bookOfWeekVos) {
            booksNumber[statistic.getCurrentWeek() - 1] = statistic.getStatistic();
        }

        System.out.println(Arrays.toString(cloth));
        System.out.println(Arrays.toString(clothWeight));
        System.out.println(Arrays.toString(booksNumber));

        HashMap<String, Object[]> hashMap = new HashMap<>(3);

        hashMap.put("clothes", cloth);
        hashMap.put("weights", clothWeight);
        hashMap.put("books", booksNumber);
        return hashMap;
    }

    @Override
    public void updateImage(Integer adminId, String adminImage) {
        adminMapper.updateImage(adminId, adminImage);
    }

    @Override
    public Admin updateAdmin(Admin admin) {
        String key = "admin:" + admin.getAccount();
        redisUtil.set(key, admin);
        adminMapper.updateAdmin(admin);
        return admin;
    }

    @Override
    public int judgeAccount(String account) {

        int flag = 0;
        String key = "list:workList";
        if (redisUtil.hasKey(key)) {
            System.out.println("redis判断账号是否存在");
            Map<Object, Object> map = redisUtil.hmget(key);
            for (Map.Entry<Object, Object> admin : map.entrySet()) {
                Admin a = (Admin) admin.getValue();
                if (a.getAccount().equals(account)) {
                    flag = 1;
                    break;
                }
            }
        } else {
            Admin admin = adminMapper.getAdminByAccount(account);
            if (admin != null) {
                flag = 1;
            }
        }
        return flag;
    }

    @Override
    public void addAdmin(Admin admin) {
        String workKey = "list:workList";
        String adminKey = "list:adminList";
        adminMapper.addAdmin(admin);
        if (redisUtil.hasKey(workKey)) {
            redisUtil.hset(workKey, admin.getAdminId().toString().toString(), admin);
        }
        if (redisUtil.hasKey(adminKey)) {
            redisUtil.hset(adminKey, admin.getAdminId().toString().toString(), admin);
        }
    }

    @Override
    public List<Admin> getWorkList(String position) {

        List<Admin> workList = new ArrayList<>();
        String key = "list:workList";
        if (redisUtil.hasKey(key)) {
            System.out.println("从redis中获取workList");
            Map<Object, Object> map = redisUtil.hmget(key);
            for (Map.Entry<Object, Object> admin : map.entrySet()) {
                workList.add((Admin) admin.getValue());
            }
        } else {
            System.out.println("从数据库中获取workList");
            Map<String, Object> map = new HashMap<>();
            workList = adminMapper.getWorkList(position);
            for (Admin admin : workList) {
                map.put(admin.getAdminId().toString(), admin);
            }
            redisUtil.hmset(key, map);
        }
        Collections.sort(workList);
        return workList;
    }

    @Override
    public List<Admin> getVolunteerList(String position) {
        List<Admin> volunteerList = new ArrayList<Admin>();
        String key = "list:volunteerList";
        if (redisUtil.hasKey(key)) {
            System.out.println("从redis中获取volunteerList");
            Map<Object, Object> map = redisUtil.hmget(key);
            for (Map.Entry<Object, Object> admin : map.entrySet()) {
                volunteerList.add((Admin) admin.getValue());
            }
        } else {
            System.out.println("从数据库中获取volunteerList");
            Map<String, Object> map = new HashMap<>();
            volunteerList = adminMapper.getWorkList(position);
            for (Admin admin : volunteerList) {
                map.put(admin.getAdminId().toString(), admin);
            }
            redisUtil.hmset(key, map);
        }

        return volunteerList;
    }

    @Override
    public List<Admin> getAdminList() {

        List<Admin> adminList = adminMapper.getAdminList();
        Collections.sort(adminList);
        return adminList;
    }


    @Override
    public void deleteAdmin(Integer adminId) {
        adminMapper.deleteAdmin(adminId);

    }

    @Override
    public void deleteVolunteer(Integer volunteerId) {
        System.out.println("删除志愿者");
        adminMapper.deleteAdmin(volunteerId);

    }

    @Override
    public Admin addVolunteer(Admin admin) {
        adminMapper.addVolunteer(admin);
        return admin;
    }

    @Override
    public void updateWorkSate(String[] idList, String workArea) {

        String key = "list:adminList";
        if (redisUtil.hasKey(key)) {
            for (String id : idList) {
                Admin admin = (Admin) redisUtil.hget(key, id);
                System.out.println(admin);
                admin.setWorkArea(workArea);
                redisUtil.hset(key, id, admin);
                adminMapper.updateWorkSate(Integer.valueOf(id), workArea);
            }
        }
    }

    @Override
    public Admin getAdminInfo(Integer adminId) {
        Admin admin = new Admin();
        String key = "list:adminList";
        if (redisUtil.hasKey(key)) {
            admin = (Admin) redisUtil.hget(key, adminId.toString());
        } else {
            admin = adminMapper.getAdminById(adminId);
        }

        return admin;
    }

    List<Integer> clothes = new ArrayList<Integer>(7) {
        {
            add(2);
            add(1);
            add(2);
            add(2);
            add(3);
            add(2);
            add(2);
        }
    };
    List<Double> weights = new ArrayList<Double>(7) {
        {
            add(0.00);
            add(1.00);
            add(2.00);
            add(0.05);
            add(1.00);
            add(1.00);
            add(1.00);
        }
    };
    List<Integer> books = new ArrayList<Integer>(7) {
        {
            add(1);
            add(3);
            add(2);
            add(1);
            add(2);
            add(2);
            add(4);
        }
    };

    Integer weekClothes = 0;
    Double weekWeight = 0.00;
    Integer weekBooks = 0;

    int week = 0;

    /**
     * 设置首页表格的数据
     */
    @Scheduled(cron = "0 58 23 * * ?")
    @Override
    public void setData() {
        Map<String, Object> map = new HashMap<>(3);
        if (week > 6) {
            week = 0;
        }
        Integer c = (Integer) redisUtil.get("totalClothesNumber") - weekClothes;
        Integer b = (Integer) redisUtil.get("totalBooksNumber") - weekBooks;
        Double w = (Double) redisUtil.get("totalWeight") - weekWeight;

        clothes.set(week, c);
        weights.set(week, w);
        books.set(week, b);

        System.out.println(week);
        System.out.println(clothes);
        System.out.println(weights);
        System.out.println(books);
        map.put("clothes", clothes);
        map.put("weights", weights);
        map.put("books", books);
        redisUtil.set("chart1", map);
        week++;
    }

    @Scheduled(cron = "0 3 0 * * ?")
    public void setDayData() {
        weekClothes = (Integer) redisUtil.get("totalClothesNumber");
        weekWeight = (Double) redisUtil.get("totalWeight");
        weekBooks = (Integer) redisUtil.get("totalBooksNumber");
    }

    List<Double> counts = new ArrayList<Double>(12) {
        {
            add(500.00);
            add(708.89);
            add(750.00);
            add(800.00);
            add(600.00);
            add(800.00);
            add(900.00);
            add(700.00);
            add(600.00);
            add(950.00);
            add(1000.00);
            add(900.00);

        }
    };

    Double monthCount = 0.00;

    /**
     * 统计每个月初的金额
     */
    @Scheduled(cron = "0 0 0 1 1/1 ?")
    public void setMonthData() {
        System.out.println("统计");
        BigDecimal b = BigDecimal.valueOf((Double) redisUtil.hget("totalAmount", "1"));
        monthCount = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(monthCount);
    }

    int month = 0;

    @Scheduled(cron = "0 59 23 28 1/1 ?")
    public void setCountData() {
        Map<String, Object> map = new HashMap<>(3);
        //0 59 23 28 1/1 ?
        if (month >= 12) {
            month = 0;
        }
        System.out.println(month);
        BigDecimal b = BigDecimal.valueOf((Double) redisUtil.hget("totalAmount", "1"));
        Double c = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        counts.set(month, c - monthCount);
        map.put("count", counts);
        redisUtil.set("chart2", map);
        month++;
    }


}
